#include "StdAfx.h"
#include "NateOnCrypt.h"

CNateOnCrypt::CNateOnCrypt(void)
:m_bAddData(FALSE)
{
}

CNateOnCrypt::~CNateOnCrypt(void)
{
}

void CNateOnCrypt::Char2Hex(unsigned char ch, char* szHex)
{
	static unsigned char saucHex[] = "0123456789ABCDEF";

	szHex[0] = saucHex[ch >> 4];
	szHex[1] = saucHex[ch&0xF];
	szHex[2] = 0;
}

BOOL CNateOnCrypt::Hex2Char(char const* szHex, unsigned char& rch)
{
	if(*szHex >= '0' && *szHex <= '9')
	{
		rch = *szHex - '0';
	}
	else if(*szHex >= 'A' && *szHex <= 'F')
	{
		rch = *szHex - 55; //-'A' + 10
	}
	else
	{
		return FALSE; //Is not really a Hex string
	}
	szHex++;

	if(*szHex >= '0' && *szHex <= '9')
	{
		(rch <<= 4) += *szHex - '0';
	}
	else if(*szHex >= 'A' && *szHex <= 'F')
	{
		(rch <<= 4) += *szHex - 55; //-'A' + 10;
	}
	else
	{
		return FALSE; //Is not really a Hex string
	}
	return TRUE;
}

void CNateOnCrypt::Binary2Hex(unsigned char const* pucBinStr, int nBinSize, char* pszHexStr)
{
	char					szHex[3];
	unsigned char const*	pucBinStr1	= pucBinStr;

	*pszHexStr = 0;

	for(int	i = 0; i < nBinSize; i++, pucBinStr1++)
	{
		Char2Hex(*pucBinStr1, szHex);
		strcat(pszHexStr, szHex);
	}
}

BOOL CNateOnCrypt::Hex2Binary(char const* pszHexStr, unsigned char* pucBinStr, int nBinSize)
{
	unsigned char ch;
	
	for(int i = 0; i < nBinSize; i++, pszHexStr += 2, pucBinStr++)
	{
		if(!Hex2Char(pszHexStr, ch))
		{
			return FALSE;
		}
		*pucBinStr = ch;
	}
	return true;
}